﻿using BCCommon;
using BCEntity.TMS.NightFreightConfiguration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.NightFreightConfiguration
{
    public class NightFreightConfigurationData : INightFreightConfigurationData
    {
        private readonly IDatabaseContext database;

        private const string QUERY = @"SELECT A.NightFreightConfigurationId,A.OrderExtractProportion,A.StartHour,A.StartMinute,A.EndHour,A.EndMinute,A.CreateTime,A.CreateAdminId,A.CreateAdminName,A.UpdateTime,A.EditAdminId,A.EditAdminName  
                                    FROM NightFreightConfiguration A";

        public NightFreightConfigurationData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<NightFreightConfigurationEntity> Insert(NightFreightConfigurationEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<(IEnumerable<NightFreightConfigurationEntity>, int)> Query(int? orderExtractProportion, int? startHour, int? startMinute, int? endHour, int? endMinute, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (orderExtractProportion.HasValue)
            {
                wheres.Add("A.OrderExtractProportion = ?OrderExtractProportion");
                parameters.Add("?OrderExtractProportion", orderExtractProportion.Value, DbType.Int32);
            }
            if (startHour.HasValue)
            {
                wheres.Add("A.StartHour >= ?StartHour");
                parameters.Add("?StartHour", startHour.Value, DbType.Int32);
            }
            if (startMinute.HasValue)
            {
                wheres.Add("A.StartMinute >= ?StartMinute");
                parameters.Add("?StartMinute", startMinute.Value, DbType.Int32);
            }
            if (endHour.HasValue)
            {
                wheres.Add("A.EndHour <= ?EndHour");
                parameters.Add("?EndHour", endHour.Value, DbType.Int32);
            }
            if (endMinute.HasValue)
            {
                wheres.Add("A.EndMinute <= ?EndMinute");
                parameters.Add("?EndMinute", endMinute.Value, DbType.Int32);
            }
            string sql = string.Format(@"{0} {1} ORDER BY NightFreightConfigurationId DESC LIMIT {2},{3};
                                    SELECT COUNT(1) FROM NightFreightConfiguration A {1}",
                                    QUERY,
                                    wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                                    (pageIndex - 1) * pageSize,
                                    pageSize);
            return await database.Slave.QueryPageableListAsync<NightFreightConfigurationEntity>(sql, parameters);
        }

        public async Task<NightFreightConfigurationEntity> GetEntity(long nightFreightConfigurationId)
        {
            return await database.Slave.QueryEntityAsync<NightFreightConfigurationEntity>($"{QUERY} WHERE A.NightFreightConfigurationId = ?NightFreightConfigurationId", new { nightFreightConfigurationId });
        }

        public async Task<NightFreightConfigurationEntity> Update(NightFreightConfigurationEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> Delete(long nightFreightConfigurationId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM NightFreightConfiguration WHERE NightFreightConfigurationId = ?NightFreightConfigurationId", new { nightFreightConfigurationId });
        }

        public async Task<IEnumerable<NightFreightConfigurationEntity>> GetList(int? orderExtractProportion, int? startHour, int? startMinute, int? endHour, int? endMinute)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (orderExtractProportion.HasValue)
            {
                wheres.Add("A.OrderExtractProportion = ?OrderExtractProportion");
                parameters.Add("?OrderExtractProportion", orderExtractProportion.Value, DbType.Int32);
            }
            if (startHour.HasValue)
            {
                wheres.Add("A.StartHour >= ?StartHour");
                parameters.Add("?StartHour", startHour.Value, DbType.Int32);
            }
            if (startMinute.HasValue)
            {
                wheres.Add("A.StartMinute >= ?StartMinute");
                parameters.Add("?StartMinute", startMinute.Value, DbType.Int32);
            }
            if (endHour.HasValue)
            {
                wheres.Add("A.EndHour <= ?EndHour");
                parameters.Add("?EndHour", endHour.Value, DbType.Int32);
            }
            if (endMinute.HasValue)
            {
                wheres.Add("A.EndMinute <= ?EndMinute");
                parameters.Add("?EndMinute", endMinute.Value, DbType.Int32);
            }
            string sql = string.Format(@"{0} {1} ORDER BY NightFreightConfigurationId DESC",
                                    QUERY,
                                    wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<NightFreightConfigurationEntity>(sql, parameters);
        }
    }
}
